#coding=utf8
import time
from binancetrade import binance
from colored import Colored



def comparison_price(symbol,interval,limit):#500个k线单位价格对比，输出涨跌幅大于15%的
    ba=binance()
    klines=ba.get_Klines(symbol,interval,limit)
    max_min_price=ba.get_max_min_price(klines)
    maxtime=max_min_price[0]
    maxpri=max_min_price[1]
    mintime=max_min_price[2]
    minpri=max_min_price[3]
    time0=time.time()
    price=ba.get_symbol_price(symbol)
    if maxtime>mintime:
        time2=mintime
        price2=minpri
        time1=maxtime
        price1=maxpri
    else:
        time2=maxtime
        price2=maxpri
        time1=mintime
        price1=minpri
    f_up_rate2=(price1-price2)/price2
    f_up_speed2=(price1-price2)*3600/(price2*(time1-time2))
    f_up_rate1=(price-price1)/price1
    f_up_speed1=(price-price1)*3600/(price1*(time0-time1))

    time0=('%s'%time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time0))).ljust(19)
    time1=('%s'%time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time1))).ljust(19)
    time2=('%s'%time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time2))).ljust(19)

    price=('%.8f'%price).ljust(19)
    price1=('%.8f'%price1).ljust(19)
    price2=('%.8f'%price2).ljust(19)

    up_speed1=('%.2f%%/h'%(f_up_speed1*100)).rjust(9)
    up_speed2=('%.2f%%/h'%(f_up_speed2*100)).rjust(9)

    up_rate1=('%.2f%%'%(f_up_rate1*100)).rjust(9)
    up_rate2=('%.2f%%'%(f_up_rate2*100)).rjust(9)

    color=Colored()
    is_ok=False
    if f_up_rate2>0.15:#最低价格到最高价涨幅大于15%
        up_rate2=color.green(up_rate2)
        is_ok=True
    if f_up_rate1>0.05:#最低价到现价涨幅大于5%
        up_rate1=color.green(up_rate1)
        is_ok=True
    if f_up_rate2<-0.15:#最低价格到最高价涨幅小于-15%
        up_rate2=color.red(up_rate2)
        is_ok=True
    if f_up_rate1<-0.05:#最低价到现价涨幅小于-5%
        up_rate1=color.red(up_rate1)
        is_ok=True

    if f_up_speed2>0.15:
        up_speed2=color.green(up_speed2)
        is_ok=True
    if f_up_speed1>0.05:
        up_speed1=color.green(up_speed1)
        is_ok=True
    if f_up_speed2<-0.15:
        up_speed2=color.red(up_speed2)
        is_ok=True
    if f_up_speed1<-0.05:
        up_speed1=color.red(up_speed1)
        is_ok=True

    if is_ok:
        info ='|----------------------------------------------------------------------------------------------------------------------|\n'
        info+='|          |  Time:%s | %s |  Time:%s | %s |  Time:%s |\n'%(time2,up_rate2,time1,up_rate1,time0)
        info+='| %s |---------------------------+-----------+---------------------------+-----------+---------------------------|\n'%symbol.center(8)
        info+='|          | Price:%s | %s | Price:%s | %s | Price:%s |'%(price2,up_speed2,price1,up_speed1,price)

        print(info)

        # itchat.send(info, wxid)#发微信消息


def main():
    interval_dict={'1m':'1分钟','5m':'5分钟','15m':'15分钟'}

    interval='5m'#K线类型
    limit=500    #数据量，最大500
    up=0.15      #最小涨幅条件
    down=0.1     #最大跌幅条件

    info='\n\n\t\t【币安BTC市场价格筛选】\n\n'

    info+='价格单位：%sK线         统计范围：%d个价格单位          筛选内容：涨幅大于%d%%，跌幅大于%d%%      数据来源：币安网\n'%(interval_dict[interval],limit,up*100,down*100)
    info+='========================================================================================================================\n'
    info+='| %s | %s | %s | %s | %s | %s |'%('交易对'.center(5),'最高(低)价格'.center(20),'涨幅(速)'.center(6),'最高(低)价格'.center(20),'涨幅(速)'.center(6),'当前价格'.center(21))

    print(info)
    ba=binance()
    for sym in ba.get_symbol_list('BTC'):
        comparison_price(sym,interval,limit)
    info='========================================================================================================================'
    print(info)

if __name__=='__main__':
    main()

